<!DOCTYPE html>
<html lang="en">
    <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <meta content="Bmob,BmobCloud,bomb,BaaS,mBaaS,PaaS,Serverless,FaaS,Function as a Service,Backend as a Service,serverless computing,cloud function,后端云,bmob后端云,小程序云,小程序后端,云数据库,云存储,文件存储,云函数,云端代码,定时任务,游戏后端,游戏云,用户系统,无服务器函数,移动开发,app开发,小程序开发,云端一体化,互联网中间件" name="keywords"/>
            <meta content="国内首家专注于移动应用Serverless云服务的平台,Bmob后端云让移动开发更简单,全方位一体化的后端服务平台" name="description"/>
            
            
            
            <link rel="shortcut icon" href="../../img/favicon.ico">
        
        <script type="text/javascript" src="//game.bmob.cn/static/doc_union.js"></script>
            <!--
            <title>Android - Bmob文档中心</title>
            -->
            <title>推送服务 &middot; Android – Bmob后端云 </title>        
            
            <link href="../../css/bootstrap-custom.min.css" rel="stylesheet">
            <link href="../../css/font-awesome-4.5.0.css" rel="stylesheet">
            <link href="../../css/base.css" rel="stylesheet">
            <link rel="stylesheet" href="../../css/highlight.css">
            <link href="../../css/agate.css" rel="stylesheet">
            <link href="../../css/custom.css" rel="stylesheet" id="custom">
    </head>

    <body >

        <div class="navbar navbar-fixed-top" role="navigation">
    <div class="main-nav">
        <!-- Collapsed navigation -->
        <div class="navbar-header">
            <a class="navbar-brand" href="../..">
                <!--Bmob文档中心-->
                <img src="../../img/logo.png" alt="">
            </a>
        </div>

        <!-- Expanded navigation -->
        <div class="navbar-collapse collapse">

          <ul class="nav navbar-nav">
            
              <li class="pull-left">
                
					<a href="../..">文档首页</a>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="">数据服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">数据服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../data/android/">Android</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/ios/">iOS</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/csharp/">C#</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/php/">PHP</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/go/">GO</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/restful/">REST API</a>
                                </li>
                            
                            
								<li >
									<a href="../../data/wechat_app_new/rm/">JavaScript</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../data/cocos2d_x/">Cocos2D-X</a>
                                </li>
							
                            
								<li >
									<a href="../../data/wechat_app/">小程序</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../data/wechat_app_new/">快应用</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../data/wechat_app_new/">Nodejs</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../data/wechat_app_new/">Cocos Creator</a>
                                </li>
							
                            
								<li >
									<a href="../../data/wechat_app_new/rm/">小程序(新)</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../data/kotlin/">Kotlin</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../data/python/">Python</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../cloud_function/android/">云函数</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">云函数 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../cloud_function/android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/java/">Java</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/python/">Python</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/csharp/">C#</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../cloud_function/restful/">REST API</a>
                                </li>
							
                            
								<li >
									<a href="../../cloud_function/web/">Web</a>
                                </li>
                            
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../sms/android/">短信服务</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">短信服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../sms/android/">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../sms/restful/">REST API</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="">游戏实时后端</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">游戏实时后端 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../game/unity/quick_start/">Unity</a>
                                </li>
                            
                            
								<li >
									<a href="../../game/cocos_creator/quick_start/">Cocos Creator</a>
                                </li>
                            
                            
								<li >
									<a href="../../game/wechat_games/quick_start/">微信小游戏</a>
                                </li>
                            
                            
                                <li >
                                    <a href="../../game/cloud_function/develop_doc/">云函数</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../game/classic_case/">经典案例</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="">即时通讯</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">即时通讯 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
								<li >
									<a href="../../im/android/">Android</a>
                                </li>
                            
                            
								<li >
									<a href="../../im/ios/">iOS</a>
                                </li>
                            
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left active">
                
					<!--<a href="./">推送服务</a>-->
					<li class="dropdown active">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">推送服务 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li class="active">
                                    <a href="./">Android</a>
                                </li>
							
                            
                                <li >
                                    <a href="../ios/">iOS</a>
                                </li>
							
                            
                                <li >
                                    <a href="../php/">PHP</a>
                                </li>
							
                            
                                <li >
                                    <a href="../javascript/">JavaScript</a>
                                </li>
							
                            
                                <li >
                                    <a href="../restful/">REST API</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li class="pull-left">
                
					<!--<a href="../../other/common_problem/">其他</a>-->
					<li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">其他 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            
                                <li >
                                    <a href="../../other/common_problem/">常见问题</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../other/error_code/">错误码</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../other/data_safety/">数据安全</a>
                                </li>
							
                            
                                <li >
                                    <a href="../../other/bql/">BQL</a>
                                </li>
							
                        </ul>
                    </li>
                
              </li>
            
              <li>
                  <a href="https://www.bmob.cn/repository/index" target="_blank">知识库</a>
              </li>
              <li>
                  <a href="http://doc.bmob.cn/video/index.html" target="_blank">视频教程</a>
              </li>
            </ul>
            
        </div>
    </div>
</div>

        <div class="pagebody" id="main-wrapper">
            <div class="sidebar">
                <div class="bs-sidebar hidden-print affix well" role="complementary">

	
		
	
		
				
				
				
				
				
				
				
				
				
				
				
				
				
				
				
		
	
		
				
				
				
				
				
				
				
				
				
		
	
		
				
				
				
				
				
		
	
		
				
				
				
				
				
		
	
		
				
				
		
	
		
				
					<div class="code-title">Android</div>
				
				
				
				
				
		
	
		
				
				
				
				
		
	


	<ul class="nav bs-sidenav">
		
			
		
			
		
			
		
			
		
			
		
			
		
			
				
					
						
							
								
									
										<li class="active "><a href="#1bmobpush-sdk">1、BmobPush SDK 简介</a>
											
										</li>
									
										<li class=""><a href="#2bmobpush-sdk">2、BmobPush SDK 集成</a>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#21sdk">2.1、集成SDK</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#22androidmanifestxml">2.2、配置AndroidManifest.xml</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#23">2.3、配置代码</a></li>
											</ul>
											
										</li>
									
										<li class=""><a href="#3">3、控制台推送消息给客户端</a>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#31">3.1、推送设置</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#32">3.2、推送消息</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#33">3.3、推送注意事项</a></li>
											</ul>
											
										</li>
									
										<li class=""><a href="#4bmobpush-sdk">4、BmobPush SDK 的使用</a>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#41">4.1、初始化设备信息</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#42installation">4.2、自定义Installation表</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#43">4.3、频道的订阅和退订</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#44">4.4、客户端广播推送消息</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#45">4.5、客户端组播推送消息</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#46">4.6、客户端多播推送消息</a></li>
											</ul>
											
											<ul class="nav nav-l2">
												<li><a class="itm-l2" href="#47">4.7、客户端点播推送消息</a></li>
											</ul>
											
										</li>
									
								
							
						
					
						
					
						
					
						
					
						
					
				
			
		
			
		
	</ul>
</div>
            </div>
            <div class="content" role="main">
                <div class="wrap">
                    

<h2 id="1bmobpush-sdk">1、BmobPush SDK 简介<a class="headerlink" href="#1bmobpush-sdk" title="Permanent link">&para;</a></h2>
<p>推送能够让你和你的应用保持联系，你可以快速而有效地通知到所有的用户，下面这个教程将会教你如何使用BmobPush SDK来推送消息。请确保您在使用BmobPush SDK之前已经了解此文档，如有疑问请加Push使用交流群<a href="">182897507</a>咨询。</p>
<h2 id="2bmobpush-sdk">2、BmobPush SDK 集成<a class="headerlink" href="#2bmobpush-sdk" title="Permanent link">&para;</a></h2>
<h3 id="21sdk">2.1、集成SDK<a class="headerlink" href="#21sdk" title="Permanent link">&para;</a></h3>
<table>
<thead>
<tr>
<th>SDK或Demo</th>
<th>下载地址</th>
</tr>
</thead>
<tbody>
<tr>
<td>数据服务 SDK</td>
<td>使用最新版本v3.6.3，集成方式请见下方的自动集成方式。手动集成下载地址：<a href="https://www.bmob.cn/downloads">https://www.bmob.cn/downloads</a></td>
</tr>
<tr>
<td>消息推送 SDK</td>
<td>使用最新版本v1.0.2，集成方式请见下方的自动集成方式。手动集成下载地址：<a href="https://www.bmob.cn/downloads">https://www.bmob.cn/downloads</a></td>
</tr>
<tr>
<td>消息推送 Demo</td>
<td><a href="https://github.com/chaozhouzhang/bmob-push-demo">https://github.com/chaozhouzhang/bmob-push-demo</a></td>
</tr>
</tbody>
</table>
<p>自动集成方式：</p>
<p>1、配置project下的build.gradle文件：</p>
<pre><code class="gradle">allprojects {
    repositories {
        jcenter()
        //Bmob的maven仓库地址
        maven { url &quot;https://raw.github.com/bmob/bmob-android-sdk/master&quot; }
    }
}
</code></pre>

<p>2、配置app下的build.gradle文件：</p>
<pre><code class="gradle">dependencies {
    //Bmob的数据服务SDK
    compile 'cn.bmob.android:bmob-sdk:3.6.3'
    //Bmob的消息推送SDK
    compile 'cn.bmob.android:bmob-push:1.0.2'
}
</code></pre>

<h3 id="22androidmanifestxml">2.2、配置AndroidManifest.xml<a class="headerlink" href="#22androidmanifestxml" title="Permanent link">&para;</a></h3>
<h4 id="221androidmanifestxml">2.2.1、在您的应用程序AndroidManifest.xml文件中添加相应的权限<a class="headerlink" href="#221androidmanifestxml" title="Permanent link">&para;</a></h4>
<p>请注意在Android 6.0版本开始某些权限需要动态获取，详情请看Android Developwers官方文档，<a href="http://developer.android.com/intl/zh-cn/about/versions/marshmallow/android-6.0-changes.html">android-6.0-changes</a>和<a href="https://developer.android.google.cn/about/versions/nougat/android-7.0-changes.html">android-7.0-changes</a>。</p>
<pre><code class="xml">    &lt;!--TODO 集成：1.1、添加数据SDK和推送SDK需要的权限--&gt;
    &lt;!--比目数据SDK所需的权限--&gt;
    &lt;!--允许联网 --&gt;
    &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
    &lt;!--获取GSM（2g）、WCDMA（联通3g）等网络状态的信息  --&gt;
    &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;
    &lt;!--获取wifi网络状态的信息 --&gt;
    &lt;uses-permission android:name=&quot;android.permission.ACCESS_WIFI_STATE&quot; /&gt;
    &lt;!--保持CPU 运转，屏幕和键盘灯有可能是关闭的,用于文件上传和下载 --&gt;
    &lt;uses-permission android:name=&quot;android.permission.WAKE_LOCK&quot; /&gt;
    &lt;!--获取sd卡写的权限，用于文件上传和下载--&gt;
    &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
    &lt;!--允许读取手机状态 用于创建BmobInstallation--&gt;
    &lt;uses-permission android:name=&quot;android.permission.READ_PHONE_STATE&quot; /&gt;


    &lt;!--推送所需的权限--&gt;
    &lt;uses-permission android:name=&quot;android.permission.RECEIVE_USER_PRESENT&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.RECEIVE_BOOT_COMPLETED&quot; /&gt;
</code></pre>

<h4 id="222androidmanifestxmlbmobpush-sdk">2.2.2、在您的应用程序AndroidManifest.xml文件中注册BmobPush SDK运行所需的推送服务和消息接收器<a class="headerlink" href="#222androidmanifestxmlbmobpush-sdk" title="Permanent link">&para;</a></h4>
<pre><code class="xml">  &lt;!--TODO 集成：1.2、添加推送所需要的服务和广播--&gt;
    &lt;service
        android:label=&quot;PushService&quot;
        android:name=&quot;cn.bmob.push.lib.service.PushService&quot;
        android:process=&quot;:bmobpush&quot;
        android:exported=&quot;true&quot;&gt;
         &lt;intent-filter&gt;
             &lt;action android:name=&quot;cn.bmob.push.lib.service.PushService&quot;/&gt;
         &lt;/intent-filter&gt;
    &lt;/service&gt;

    &lt;!-- 用于进程保活 --&gt;
    &lt;service
        android:name=&quot;cn.bmob.push.lib.service.PushNotifyService&quot;
        android:process=&quot;:bmobpush&quot; &gt;
    &lt;/service&gt;

    &lt;receiver android:name=&quot;cn.bmob.push.PushReceiver&quot; &gt;
        &lt;intent-filter&gt;
            &lt;!-- 系统启动完成后会调用 --&gt;
            &lt;action android:name=&quot;android.intent.action.BOOT_COMPLETED&quot; /&gt;               
            &lt;!-- 解锁完成后会调用 --&gt;
            &lt;action android:name=&quot;android.intent.action.USER_PRESENT&quot; /&gt;
            &lt;!-- 监听网络连通性 --&gt;
            &lt;action android:name=&quot;android.net.conn.CONNECTIVITY_CHANGE&quot; /&gt;               
        &lt;/intent-filter&gt;
    &lt;/receiver&gt;

    &lt;!-- 第3步中创建的消息接收器，在这里进行注册 --&gt;
    &lt;receiver android:name=&quot;your.package.MyPushMessageReceiver&quot;&gt;
         &lt;intent-filter &gt;
              &lt;action android:name=&quot;cn.bmob.push.action.MESSAGE&quot;/&gt;
         &lt;/intent-filter&gt;
    &lt;/receiver&gt;

    &lt;!-- 接收心跳和唤醒的广播，要和PushService运行在同个进程 --&gt;
    &lt;receiver
         android:name=&quot;cn.bmob.push.PushNotifyReceiver&quot;
         android:process=&quot;:bmobpush&quot; &gt;
         &lt;intent-filter&gt;
             &lt;!-- 接收心跳广播的action --&gt;
             &lt;action android:name=&quot;cn.bmob.push.action.HEARTBEAT&quot; /&gt;
             &lt;!-- 接收唤醒广播的action --&gt;
             &lt;action android:name=&quot;cn.bmob.push.action.NOTIFY&quot; /&gt;
         &lt;/intent-filter&gt;
    &lt;/receiver&gt;
</code></pre>

<h3 id="23">2.3、配置代码<a class="headerlink" href="#23" title="Permanent link">&para;</a></h3>
<h4 id="231">2.3.1、在你的应用程序中创建一个消息接收器<a class="headerlink" href="#231" title="Permanent link">&para;</a></h4>
<p>Push消息通过<code>action=cn.bmob.push.action.MESSAGE</code>的Intent把数据发送给客户端<code>your.package.MyPushMessageReceiver</code>,消息格式由应用自己决定，PushService只负责把服务器下发的消息以字符串格式透传给客户端。</p>
<p><code>your.package.MyPushMessageReceiver</code>的代码示例如下：</p>
<pre><code class="java">//TODO 集成：1.3、创建自定义的推送消息接收器，并在清单文件中注册
public class MyPushMessageReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if(intent.getAction().equals(PushConstants.ACTION_MESSAGE)){
            Log.d(&quot;bmob&quot;, &quot;客户端收到推送内容：&quot;+intent.getStringExtra(&quot;msg&quot;));
        }
    }

}
</code></pre>

<h4 id="232">2.3.2、启动推送服务<a class="headerlink" href="#232" title="Permanent link">&para;</a></h4>
<p>在你的应用程序主Application中调用如下方法：</p>
<pre><code class="java">//TODO 集成：1.4、初始化数据服务SDK、初始化设备信息并启动推送服务
// 初始化BmobSDK
Bmob.initialize(this, &quot;你的Application Id&quot;);
// 使用推送服务时的初始化操作
BmobInstallationManager.getInstance().initialize(new InstallationListener&lt;BmobInstallation&gt;() {
            @Override
            public void done(BmobInstallation bmobInstallation, BmobException e) {
                if (e == null) {
                    Logger.i(bmobInstallation.getObjectId() + &quot;-&quot; + bmobInstallation.getInstallationId());
                } else {
                    Logger.e(e.getMessage());
                }
            }
        });
// 启动推送服务
BmobPush.startWork(this);
</code></pre>

<p>代码中的"你的Application Id"就是你在Bmob后台中创建的应用程序的Application Id，如果你不知道这是什么，可以参考<a href="https://docs.bmob.cn/data/Android/a_faststart/doc/index.html">快速入门文档</a>中的注册Bmob账号部分。</p>
<h2 id="3">3、控制台推送消息给客户端<a class="headerlink" href="#3" title="Permanent link">&para;</a></h2>
<h3 id="31">3.1、推送设置<a class="headerlink" href="#31" title="Permanent link">&para;</a></h3>
<p>在应用面板--&gt;消息推送--&gt;推送设置界面中填写包名进行保存。
<img alt="" src="./image/setting.png" /></p>
<h3 id="32">3.2、推送消息<a class="headerlink" href="#32" title="Permanent link">&para;</a></h3>
<p>完成设置后，你可以运行应用程序，从控制台送一条消息给客户端。
<img alt="" src="./image/pushmsg.png" /></p>
<h3 id="33">3.3、推送注意事项<a class="headerlink" href="#33" title="Permanent link">&para;</a></h3>
<p>在后台推送消息给Android和iOS两个平台的时候，有一些需要注意的：
1、由于Android和iOS的提送机制不同，iOS要经过APNS，Android的推送完全是走Bmob的长连接服务，为兼容这个问题，如果你选择发送格式为“json”格式时，需要添加APNS兼容头部（见下面json的aps部分），其中，sound是iOS接收时的声音，badge是iOS通知栏的累计消息数，推送内容格式如下：</p>
<pre><code>{
    &quot;aps&quot;: {
    &quot;sound&quot;: &quot;cheering.caf&quot;, 
    &quot;alert&quot;: &quot;这个是通知栏上显示的内容&quot;, 
    &quot;badge&quot;: 0 
    }, 
    &quot;xx&quot; : &quot;json的key-value对，你可以根据情况添加更多的，客户端进行解析获取&quot;, 
}
</code></pre>

<p>2、如果你选择发送格式为“text”时，推送内容为“推送消息测试”，Bmob会自动添加aps部分发送给APNS，，相当于自动生成如下的json格式的推送内容：</p>
<pre><code>{
    &quot;aps&quot;: {
        &quot;alert&quot;: &quot;推送消息测试&quot;, 
    }
}
</code></pre>

<p>同时，也会发送给Android端，相当于自动生成如下的json格式的推送内容：</p>
<pre><code>{
    &quot;alert&quot; : &quot;推送消息测试&quot;, 
}
</code></pre>

<p>3、如果只是发送给Android端，大家可以自定义json格式的数据。</p>
<p>4、由于iOS的APNS的推送的大小是有限制的，默认最多256bytes，因此,如果你需要跨平台互通的话，需注意推送的内容不要太长。</p>
<h1 id="4bmobpush-sdk">4、BmobPush SDK 的使用<a class="headerlink" href="#4bmobpush-sdk" title="Permanent link">&para;</a></h1>
<h2 id="41">4.1、初始化设备信息<a class="headerlink" href="#41" title="Permanent link">&para;</a></h2>
<h3 id="411bmobinstallation">4.1.1、设备信息表：BmobInstallation<a class="headerlink" href="#411bmobinstallation" title="Permanent link">&para;</a></h3>
<p>使用消息推送前，首先需要初始化设备信息。</p>
<pre><code class="java">BmobInstallationManager.getInstance().initialize(new InstallationListener&lt;BmobInstallation&gt;() {
            @Override
            public void done(BmobInstallation bmobInstallation, BmobException e) {
                if (e == null) {
                    Logger.i(bmobInstallation.getObjectId() + &quot;-&quot; + bmobInstallation.getInstallationId());
                } else {
                    Logger.e(e.getMessage());
                }
            }
        });
</code></pre>

<p>初始化成功后，数据服务SDK会自动生成一个BmobInstallation表，它包含了推送所需要的所有信息。例如，如果你有一个棒球的App，你可以让用户订阅感兴趣的棒球队，把订阅的棒球队名称放置到BmobInstallation表的channels数组中，然后及时地将这个球队的消息推送给订阅的用户。</p>
<table>
<thead>
<tr>
<th>字段名称</th>
<th>解释</th>
</tr>
</thead>
<tbody>
<tr>
<td>channels</td>
<td>当前这个设备订阅的渠道名称数组</td>
</tr>
<tr>
<td>timeZone</td>
<td>设备所在位置的时区， 如Asia/Shanghai，这个会在每个BmobInstallation对象更新时同步（只读）</td>
</tr>
<tr>
<td>deviceType</td>
<td>设备的的类型, 值为："ios" 或 "android" (只读)</td>
</tr>
<tr>
<td>installationId</td>
<td>Bmob使用的设备唯一号 (只读)</td>
</tr>
</tbody>
</table>
<h3 id="412bmobinstallationmanager">4.1.2、设备信息表管理：BmobInstallationManager<a class="headerlink" href="#412bmobinstallationmanager" title="Permanent link">&para;</a></h3>
<p>获取设备唯一标志：</p>
<pre><code class="java">BmobInstallationManager.getInstallationId();
</code></pre>

<p>获取当前设备信息：</p>
<pre><code class="java">BmobInstallationManager.getInstance().getCurrentInstallation();
</code></pre>

<h2 id="42installation">4.2、自定义Installation表<a class="headerlink" href="#42installation" title="Permanent link">&para;</a></h2>
<p>开发者如果想要为设备信息表增加其他属性，则可以通过继承BmobInstallation类的方式来完成，用来定制更通用的推送。</p>
<p>举例：</p>
<pre><code class="java">public class Installation extends BmobInstallation {

    private User user;
    private BmobGeoPoint location;


    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public BmobGeoPoint getLocation() {
        return location;
    }

    public void setLocation(BmobGeoPoint location) {
        this.location = location;
    }
}
</code></pre>

<p>那么如何更新增加的<code>user</code>字段的值呢？</p>
<p>示例如下：</p>
<pre><code class="java">    /**
     * 修改设备表的用户信息：先查询设备表中的数据，再修改数据中用户信息
     * @param user
     */
    private void modifyInstallationUser(final User user) {
        BmobQuery&lt;Installation&gt; bmobQuery = new BmobQuery&lt;&gt;();
        final String id = BmobInstallationManager.getInstallationId();
        bmobQuery.addWhereEqualTo(&quot;installationId&quot;, id);
        bmobQuery.findObjectsObservable(Installation.class)
                .subscribe(new Action1&lt;List&lt;Installation&gt;&gt;() {
                    @Override
                    public void call(List&lt;Installation&gt; installations) {

                        if (installations.size() &gt; 0) {
                            Installation installation = installations.get(0);
                            installation.setUser(user);
                            installation.updateObservable()
                                    .subscribe(new Action1&lt;Void&gt;() {
                                        @Override
                                        public void call(Void aVoid) {
                                            toastI(&quot;更新设备用户信息成功！&quot;);
                                        }
                                    }, new Action1&lt;Throwable&gt;() {
                                        @Override
                                        public void call(Throwable throwable) {
                                            toastE(&quot;更新设备用户信息失败：&quot; + throwable.getMessage());
                                        }
                                    });

                        } else {
                            toastE(&quot;后台不存在此设备Id的数据，请确认此设备Id是否正确！\n&quot; + id);
                        }

                    }
                }, new Action1&lt;Throwable&gt;() {
                    @Override
                    public void call(Throwable throwable) {
                        toastE(&quot;查询设备数据失败：&quot; + throwable.getMessage());
                    }
                });
    }
</code></pre>

<h2 id="43">4.3、频道的订阅和退订<a class="headerlink" href="#43" title="Permanent link">&para;</a></h2>
<h3 id="431">4.3.1、订阅频道<a class="headerlink" href="#431" title="Permanent link">&para;</a></h3>
<p>订阅频道可使用 <strong>subscribe</strong> 方法</p>
<pre><code class="java">BmobInstallationManager.getInstance().subscribe(Arrays.asList(&quot;NBA&quot;, &quot;CBA&quot;, &quot;IJK&quot;, &quot;NBA&quot;, &quot;CBA&quot;, &quot;USA&quot;), new InstallationListener&lt;BmobInstallation&gt;() {
                    @Override
                    public void done(BmobInstallation bmobInstallation, BmobException e) {
                        if (e == null) {
                            toastI(&quot;批量订阅成功&quot;);
                        } else {
                            toastE(e.getMessage());
                        }
                    }
                });
</code></pre>

<p>Bmob SDK对频道订阅增加去重操作，也就是说：即使你调用subscribe方法订阅了多个相同的频道，Bmob只会记录一个频道。</p>
<h3 id="432">4.3.2、退订频道<a class="headerlink" href="#432" title="Permanent link">&para;</a></h3>
<p>退订频道可使用 <strong>unsubscribe</strong> 方法</p>
<pre><code class="java">BmobInstallationManager.getInstance().unsubscribe(Arrays.asList(&quot;CBA&quot;, &quot;USA&quot;), new InstallationListener&lt;BmobInstallation&gt;() {
                    @Override
                    public void done(BmobInstallation bmobInstallation, BmobException e) {
                        if (e == null) {
                            toastI(&quot;批量取消订阅成功&quot;);
                        } else {
                            toastE(e.getMessage());
                        }
                    }
                });
</code></pre>

<h3 id="433">4.3.3、获取已经订阅的频道<a class="headerlink" href="#433" title="Permanent link">&para;</a></h3>
<p>获取已经订阅的频道可以使用<strong>getCurrentInstallation()</strong>方法</p>
<pre><code class="java">BmobInstallation bmobInstallation = BmobInstallationManager.getInstance().getCurrentInstallation();
                List&lt;String&gt; channels = bmobInstallation.getChannels();
                if (channels.size() &lt; 1) {
                    toastI(&quot;您没有订阅任何频道！&quot;);
                } else {
                    for (String channel : channels) {
                        mTvChannel.append(channel + &quot;\n&quot;);
                    }
                }
</code></pre>

<h2 id="44">4.4、客户端广播推送消息<a class="headerlink" href="#44" title="Permanent link">&para;</a></h2>
<p>和控制台推送消息给客户端一样，在客户端推送消息也需要进行推送的包名设置，请在应用面板--&gt;消息推送--&gt;推送设置界面中填写包名进行保存。</p>
<p>在客户端实现推送消息的功能，通过 <strong>BmobPushManager</strong> 对象来完成，比如给所有设备推送消息：</p>
<pre><code class="java">    BmobPushManager bmobPushManager = new BmobPushManager();
    bmobPushManager.pushMessageAll(&quot;消息内容&quot;, new PushListener() {
        @Override
        public void done(BmobException e) {
            if (e==null){
                Logger.e(&quot;推送成功！&quot;);
            }else {
                Logger.e(&quot;异常：&quot; + e.getMessage());
            }
        }
    });
</code></pre>

<h2 id="45">4.5、客户端组播推送消息<a class="headerlink" href="#45" title="Permanent link">&para;</a></h2>
<pre><code class="java">                BmobPushManager bmobPushManager = new BmobPushManager();
                BmobQuery&lt;BmobInstallation&gt; query = BmobInstallation.getQuery();
                List&lt;String&gt; channels = new ArrayList&lt;&gt;();
                //TODO 替换成你需要推送的所有频道，推送前请确认已有设备订阅了该频道，也就是channels属性存在该值
                channels.add(&quot;NBA&quot;);
                query.addWhereContainedIn(&quot;channels&quot;, channels);
                bmobPushManager.setQuery(query);
                bmobPushManager.pushMessage(&quot;消息内容&quot;, new PushListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Logger.e(&quot;推送成功！&quot;);
                        } else {
                            Logger.e(&quot;异常：&quot; + e.getMessage());
                        }
                    }
                });
</code></pre>

<h2 id="46">4.6、客户端多播推送消息<a class="headerlink" href="#46" title="Permanent link">&para;</a></h2>
<h3 id="461">4.6.1、根据平台做推送<a class="headerlink" href="#461" title="Permanent link">&para;</a></h3>
<p>给Android平台的终端推送：</p>
<pre><code class="java">                BmobPushManager bmobPushManager = new BmobPushManager();
                BmobQuery&lt;BmobInstallation&gt; query = BmobInstallation.getQuery();
                //TODO 属性值为android
                query.addWhereEqualTo(&quot;deviceType&quot;, &quot;android&quot;);
                bmobPushManager.setQuery(query);
                bmobPushManager.pushMessage(&quot;消息内容&quot;, new PushListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Logger.e(&quot;推送成功！&quot;);
                        } else {
                            Logger.e(&quot;异常：&quot; + e.getMessage());
                        }
                    }
                });
</code></pre>

<p>给IOS平台的终端推送：</p>
<pre><code class="java">                BmobPushManager bmobPushManager = new BmobPushManager();
                BmobQuery&lt;BmobInstallation&gt; query = BmobInstallation.getQuery();
                //TODO 属性值为ios
                query.addWhereEqualTo(&quot;deviceType&quot;, &quot;ios&quot;);
                bmobPushManager.setQuery(query);
                bmobPushManager.pushMessage(&quot;消息内容&quot;, new PushListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Logger.e(&quot;推送成功！&quot;);
                        } else {
                            Logger.e(&quot;异常：&quot; + e.getMessage());
                        }
                    }
                });
</code></pre>

<h3 id="462">4.6.2、根据地理位置信息推送<a class="headerlink" href="#462" title="Permanent link">&para;</a></h3>
<pre><code class="java">                BmobPushManager bmobPushManager = new BmobPushManager();
                BmobQuery&lt;BmobInstallation&gt; query = BmobInstallation.getQuery();
                //TODO 替换你需要推送的地理位置的经纬度和范围，发送前请确认installation表中已有location的BmobGeoPoint类型属性
                query.addWhereWithinRadians(&quot;location&quot;, new BmobGeoPoint(113.385610000, 23.0561000000), 1.0);
                bmobPushManager.setQuery(query);
                bmobPushManager.pushMessage(&quot;消息内容&quot;, new PushListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Logger.e(&quot;推送成功！&quot;);
                        } else {
                            Logger.e(&quot;发送前请确认installation表中已有location的BmobGeoPoint类型属性&quot;);
                            Logger.e(&quot;异常：&quot; + e.getMessage());
                        }
                    }
                });
</code></pre>

<h3 id="463">4.6.3、推送消息给不活跃的设备<a class="headerlink" href="#463" title="Permanent link">&para;</a></h3>
<pre><code class="java">                BmobPushManager bmobPushManager = new BmobPushManager();
                BmobQuery&lt;BmobInstallation&gt; query = BmobInstallation.getQuery();
                //TODO 替换你需要的判断为不活跃的时间点
                query.addWhereLessThan(&quot;updatedAt&quot;, new BmobDate(new Date()));
                bmobPushManager.setQuery(query);
                bmobPushManager.pushMessage(&quot;消息内容&quot;, new PushListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Logger.e(&quot;推送成功！&quot;);
                        } else {
                            Logger.e(&quot;异常：&quot; + e.getMessage());
                        }
                    }
                });
</code></pre>

<h3 id="464">4.6.4、根据查询条件做推送<a class="headerlink" href="#464" title="Permanent link">&para;</a></h3>
<pre><code class="java">               //TODO 替换成你作为判断需要推送的属性名和属性值，推送前请确认installation表已有该属性
                query.addWhereEqualTo(&quot;替换成你作为判断需要推送的属性名&quot;, &quot;替换成你作为判断需要推送的属性值&quot;);
                bmobPushManager.setQuery(query);
                bmobPushManager.pushMessage(&quot;消息内容&quot;, new PushListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Logger.e(&quot;推送成功！&quot;);
                        } else {
                            Logger.e(&quot;异常：&quot; + e.getMessage());
                        }
                    }
                });
</code></pre>

<h2 id="47">4.7、客户端点播推送消息<a class="headerlink" href="#47" title="Permanent link">&para;</a></h2>
<p>发送给Android单个客户端：</p>
<pre><code class="java">                //TODO 替换成所需要推送的Android客户端installationId
                BmobPushManager bmobPushManager = new BmobPushManager();
                BmobQuery&lt;BmobInstallation&gt; query = BmobInstallation.getQuery();
                String installationId = &quot;【替换你需要的id】其他Android客户端installationId&quot;;
                query.addWhereEqualTo(&quot;installationId&quot;, installationId);
                bmobPushManager.setQuery(query);
                bmobPushManager.pushMessage(&quot;消息内容&quot;, new PushListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Logger.e(&quot;推送成功！&quot;);
                        } else {
                            Logger.e(&quot;异常：&quot; + e.getMessage());
                        }
                    }
                });
</code></pre>

<p>发送给iOS单个客户端：</p>
<pre><code class="java">                //TODO 替换成所需要推送的iOS客户端deviceToken
                BmobPushManager bmobPushManager = new BmobPushManager();
                BmobQuery&lt;BmobInstallation&gt; query = BmobInstallation.getQuery();
                String deviceToken = &quot;替换成所需要推送的iOS客户端deviceToken&quot;;
                query.addWhereEqualTo(&quot;deviceToken&quot;, deviceToken);
                bmobPushManager.setQuery(query);
                bmobPushManager.pushMessage(&quot;消息内容&quot;, new PushListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Logger.e(&quot;推送成功！&quot;);
                        } else {
                            Logger.e(&quot;异常：&quot; + e.getMessage());
                        }
                    }
                });
</code></pre>
                </div>
            </div>
        </div>

        
            <script>var base_url = '../..';</script>
            <script src="../../js/jquery-1.10.2.min.js"></script>
            <script src="../../js/bootstrap-3.0.3.min.js"></script>
            <script src="../../js/highlight.pack.js"></script>
            <script src="../../js/main.js"></script>
            <script src="../../js/base.js"></script>

        <div id="go-top"><i class="fa fa-chevron-up"></i></div>
        <a href="https://docs.bmob.cn/data/Android/a_faststart/doc/index.html" id="back" target="_blank">返回 <br> 旧版</a>
    </body>

</html>